package com.itheima.health.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.MyException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author:ZZZ
 * @Date: 2021/1/19 10:40
 * @Version 1.0
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;


    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public void deleteById(int id) {
//        如果角色有关联就不能删除
        Integer count = permissionDao.findt_role_permissionById(id);
       if(count >0){
           throw new MyException("权限被角色关联了不能删除！！！");
       }
//       可以删除
        permissionDao.deleteById(id);
    }

    @Override
    public PageResult<Permission> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 条件查询
        if(StringUtils.isNotEmpty(queryPageBean.getQueryString())){
            // 有查询条件， 模糊查询
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        // page extends arrayList
        Page<Permission> page = permissionDao.findByCondition(queryPageBean.getQueryString());
        PageResult<Permission> pageResult = new PageResult<Permission>(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }

    @Override
    public Permission findById(int id) {
       return  permissionDao.findById(id);
    }


    /**
     * 查询所有权限
     *
     * @return
     */
    @Override
    public List<Permission> findPermissionAll() {
        return permissionDao.findPermissionAll();

    }
}
